package com.whccb.apps.office.document.util;

import java.sql.Timestamp;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Calendar;
import java.util.Date;

public class DateUtils {

	public static final String DATE_DFT_PATTERN = "yyyy-MM-dd";
	public static final String DATE_DFT_PATTERN_EXT1 = "yyyy/MM/dd";
	public static final String DATE_DFT_PATTERN_EXT2 = "MM/dd";
	public static final String DATETIME_DFT_PATTERN = "yyyy-MM-dd HH:mm:ss";
	public static final String DATE_MD_PATTERN = "MM-dd";

	public static String getCurrentDate() {
		return getCurrentDate(new Date(), DATE_DFT_PATTERN);
	}

	public static String getCurrentDate(String pattern) {
		return getCurrentDate(new Date(), pattern);
	}

	public static String getCurrentDate(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String getCurrentDateTime() {
		return format(new Date(), DATETIME_DFT_PATTERN);
	}

	public static String getCurrentDateTime(String pattern) {
		return format(new Date(), pattern);
	}

	public static String format(long datetime) {
		Date date = new Date(datetime);
		return format(date);
	}

	public static String format(long datetime, String pattern) {
		Date date = new Date(datetime);
		return format(date, pattern);
	}

	public static String format(Date date) {
		return format(date, DATE_DFT_PATTERN);
	}

	public static String format(Date date, String pattern) {
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		return sdf.format(date);
	}

	public static String format(String dateStr) {
		Date date = parse(dateStr, DATETIME_DFT_PATTERN);
		return format(date, DATETIME_DFT_PATTERN);
	}

	public static String format(String dateStr, String pattern) {
		Date date = parse(dateStr, DATETIME_DFT_PATTERN);
		return format(date, pattern);
	}

	public static String format(String dateStr, String orgPattern, String pattern) {
		Date date = parse(dateStr, orgPattern);
		return format(date, pattern);
	}

	public static Date parse(String dateStr) {
		return parse(dateStr, DATE_DFT_PATTERN);
	}

	public static Date parse(String dateStr, String pattern) {
		Date date = null;
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		try {
			date = sdf.parse(dateStr);
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return date;
	}

	public static Date parse(Date date) {
		return parse(date, DATE_DFT_PATTERN);
	}

	public static Date parse(Date date, String pattern) {
		String dateStr = format(date, DATETIME_DFT_PATTERN);
		return parse(dateStr, pattern);
	}

	public static Date parse(long datetime) {
		return new Date(datetime);
	}

	public static Date parse(long datetime, String pattern) {
		Date date = new Date(datetime);
		return parse(date, pattern);
	}

	public static String parseStr(String dateStr, String pattern) {
		Date date = parse(dateStr);
		return format(date, pattern);
	}

	/**
	 * 方法功能：该月的第一天. <BR>
	 * 
	 * @param date
	 * @return
	 * @see com.actionsoft.apps.dailylog.util.DateUtils
	 * @since
	 */
	public static Date getFirstOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	public static Date getFirstOfMonth(long datetime) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(datetime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		return cal.getTime();
	}

	public static Date getLastOfMonth(Date date) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	public static Date getLastOfMonth(long datetime) {
		Calendar cal = Calendar.getInstance();
		cal.setTimeInMillis(datetime);
		cal.set(Calendar.DAY_OF_MONTH, 1);
		cal.add(Calendar.MONTH, 1);
		cal.add(Calendar.DAY_OF_MONTH, -1);
		return cal.getTime();
	}

	public static Date addDate(Date date, int field, int amount) {
		Calendar cal = Calendar.getInstance();
		cal.setTime(date);
		cal.add(field, amount);
		return cal.getTime();
	}

	public static void main(String[] args) {
	}

	/**
	 * 将字符串转换成Timestamp 例如2014-10-10 yyyy-MM-dd
	 * 
	 * @param timestr日期字符串
	 * @param pattern格式字符串
	 * @return
	 * @author fulp
	 */
	public static Timestamp strToTimeStamp(String timestr, String pattern) {
		if ("".equals(timestr)) {
			timestr = "1970-01-01";
		}
		SimpleDateFormat sdf = new SimpleDateFormat(pattern);
		Timestamp ts = null;
		try {
			sdf.setLenient(false);
			ts = new Timestamp(sdf.parse(timestr).getTime());
		} catch (ParseException e) {
			e.printStackTrace();
		}
		return ts;
	}

	/**
	 * 根据输入的日期字符串 和 提前天数 ， 获得 指定日期提前几天的日期对象
	 * 
	 * @param datestr 日期对象 ，格式如2014-12-12
	 * @param beforeDays 倒推的天数
	 * @return 指定日期倒推指定天数后的日期对象
	 * @throws ParseException
	 */
	public static Date getDateByDays(String datestr, int beforeDays) throws ParseException {
		SimpleDateFormat dateFormat = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		Date inputDate = dateFormat.parse(datestr);
		Calendar cal = Calendar.getInstance();
		cal.setTime(inputDate);
		int inputDayOfYear = cal.get(Calendar.DAY_OF_YEAR);
		cal.set(Calendar.DAY_OF_YEAR, inputDayOfYear - beforeDays);
		return cal.getTime();
	}

	/**
	 * 得到当前时间的后几天
	 * 
	 * @return
	 */
	public static Date getDateByDays2(int num) {
		Calendar c = Calendar.getInstance();
		c.add(Calendar.DAY_OF_MONTH, num);
		return c.getTime();
	}

}
